package development.app;

import iplayparticle.view.iplayparticleResources;

import java.util.Vector;

import state.Clip;
import state.DrawListener;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.SMSHandler;
import state.SendSMSListener;
import state.Sprite;
import state.component.Checkbox;
import state.component.Event;
import state.component.EventListener;
import state.component.GameScrollBar;
import state.component.MultiTab;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.socket.MapDataPackage;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.NumberUtil;
import state.util.array.ByteArray;
import state.util.array.StringArray;
import state.util.array.TextFieldArray;
import development.Constants;
import development.DrawUtils;
import development.Global;
import development.Utils;
import development.app.football.FrmBettingMenu;
import development.bean.Category;
import development.bean.SMSServiceCommand;
import development.bean.Subscription;
import development.group.FrmGroupBongDaXoSo;
import development.group.FrmGroupMenu;
import development.group.FrmGroupNapXuAndExchangeGold;

public class FrmLottery extends GameForm implements EventListener, SendSMSListener {
  public static final int LOTTERY_TYPE_LIST_REQUEST = 1300;
  public static final int LOTTERY_TYPE_LIST_RESPONSE = 1301;
  public static final int LOTTERY_SCHEDULE_REQUEST = 1302;
  public static final int LOTTERY_SCHEDULE_RESPONSE = 1303;
  public static final int LOTTERY_RESULT_REQUEST = 1304;
  public static final int LOTTERY_RESULT_RESPONSE = 1305;
  public static final int LOTTERY_BET_REQUEST = 1306;
  public static final int LOTTERY_BET_RESPONSE = 1307;
  public static final int LOTTERY_PHASE_REQUEST = 1308;
  public static final int LOTTERY_PHASE_RESPONSE = 1309;
  public static final int LOTTERY_BET_BRAND_LIST_REQUEST = 1310;
  public static final int LOTTERY_BET_BRAND_LIST_RESPONSE = 1311;
  public static final int LOTTERY_BET_BRAND_REQUEST = 1312;
  public static final int LOTTERY_BET_BRAND_RESPONSE = 1313;

  public static final String SHOW_FORM_BETTING_ONLINE = "ShowFormBettingOnline";
  public static final String SHOW_FORM_BETTING_BRAND_LIST = "ShowFormBettingBrandList";

  private ImageText text8;
  private ImageText text11;
  private GameImage buttonImage;
  private int chooseParentIndex;
  private int chooseIndex;
  private int tempIndex;
  private int currentAction;
  private String type;
  private boolean showPhangOnlineForm;
  private final int DEFAULT_TEXT_X = 5;
  private int CATEGORY_IMAGE_HEIGHT;
  private int dy;
  private Hashtable logAction;
  private GameScrollBar scrollBar;
  private Clip textView;
  private Vector lotteryList;
  private TextFieldArray txtNumber;
  private TextFieldArray txtBetMoney;
  private Checkbox cbLo;
  private Checkbox cbDe;
  private Sprite betButtonSprite;
  private Hashtable parameters;

  private final String WAITING_MESSAGE = "Xin chờ trong giây lát...";
  private int packageNumber;
  private int totalPackage;
  private int percent;
  
  private String SMS_LOTTERY_BRAND;

  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmLottery();
    }
    return singleton;
  }

  public void init(Hashtable parameters1) {
    this.parameters = parameters1;
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    text11 = FontManager.getFont(FontManager.FONT_SIZE_11);
    buttonImage = new GameImage(iplayparticleResources.image_button, null, null);
    betButtonSprite = new Sprite(null, _manager, 0, 0, Constants.TOP_LEFT_ANCHOR);
    betButtonSprite.setImage(buttonImage);
    betButtonSprite.setAnchor(Constants.TOP_LEFT_ANCHOR);
    betButtonSprite.setVisible(false);
    betButtonSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        text8.drawString2(g, "Phang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
      }
    });
    CATEGORY_IMAGE_HEIGHT = Global.getDefaultAvatar().getHeight();
    dy = DrawUtils.getTopBarHeight();
    textView = new Clip(DEFAULT_TEXT_X, dy, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT - dy - 10);
    scrollBar = new GameScrollBar(Constants.SCREEN_WIDTH - 5, 25, 5, Constants.SCREEN_HEIGHT - 50, textView, _manager);
    logAction = new Hashtable();
    currentAction = 1;
    lotteryList = new Vector();
    type = "";
    showPhangOnlineForm = false;
    chooseParentIndex = 0;
    chooseIndex = 0;
    tempIndex = 0;
    txtNumber = TextFieldArray.create(3);
    txtBetMoney = TextFieldArray.create(3);
    for (int i = 0; i < 3; i++) {
      TextField txtNum = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
      txtNum.setSize(GameConstants.SCREEN_WIDTH / 2 - 20, text8.getHeight());
      txtNum.setVisible(false);
      txtNumber.set(i, txtNum);
      

      TextField textFieldBetMoney = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
      textFieldBetMoney.setSize(GameConstants.SCREEN_WIDTH / 2 - 20, text8.getHeight());
      textFieldBetMoney.setVisible(false);
      txtBetMoney.set(i, textFieldBetMoney);
    }
    txtNumber.array[0].setFocused(true);
    cbLo = new Checkbox(_manager, this, Constants.CHECK_BOX_STYLE_APP, 0, 0);
    cbLo.setLabel("Lô  ($x3.5)");
    cbLo.setVisible(false);
    cbDe = new Checkbox(_manager, this, Constants.CHECK_BOX_STYLE_APP, 0, 0);
    cbDe.setVisible(false);
    cbDe.setLabel("Đề ($x70)");

    if (parameters1 != null) {
      String requestType = (String) parameters1.get("RequestType");
      if (requestType != null && requestType.equals("PHANG_ONLINE")) {
        String formType = (String) parameters1.get("formType");
        if (formType.equals(SHOW_FORM_BETTING_ONLINE)) {
          showPhangOnlineForm = true;
        } else if (formType.equals(SHOW_FORM_BETTING_BRAND_LIST)) {
          request(LOTTERY_BET_BRAND_LIST_REQUEST, new Vector());
        }
      } else {
        request(LOTTERY_TYPE_LIST_REQUEST, new Vector());
      }
    } else {
      request(LOTTERY_TYPE_LIST_REQUEST, new Vector());
    }
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);
    if (lotteryList.isEmpty() && !showPhangOnlineForm) {
      text8.drawString2(g, WAITING_MESSAGE, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.TOP_HCENTER_ANCHOR);
    }
    if (type.equals("lotteryTypeList")) {
      drawLotteryType(g);
    } else if (type.equals("XSTT_LIST") || type.equals("BT_LIST") || type.equals("CAU_LIST") || type.equals("LOTTERY_SCHEDULE_LIST") || type.equals("NGU_HANH_LIST")) {
      drawLotterySchedule(g);
    } else if (type.equals("XSTT")) {
      // drawLotteryResult(g);
      drawLotteryResultOther(g);
    } else if (type.equals("BET_BRAND_LIST")) {
      drawPhangOnlineBrandList(g);
    } else if (type.equals("PHANG_ONLINE")) {
      drawPhase(g);
    } else {
      drawLotteryResultOther(g);
    }
    if (showPhangOnlineForm) {
      text8.drawString2(g, "Phang online", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
      drawPhangOnlineForm(g);
      DrawUtils.getBottomBar(_manager).setCenterLabel("Chọn");
    }
  }

  private void drawPhase(Graphics g) {
    int textY = dy;
    int index = 0;
    for (int i = 0; i < lotteryList.size(); i++) {
      Category category = (Category) lotteryList.elementAt(i);
      if (chooseIndex == index) {
        g.setColor(0x53A4CF);
        g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, textY + 55);
      }
      text8.drawString3(g, category.name, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      text8.drawString3(g, (String) category.exParam.elementAt(0), GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      text8.drawString3(g, (String) category.exParam.elementAt(1), GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      if (chooseIndex == index) {
        g.setColor(0x53A4CF);
        g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight() + 5);
        text8.drawString3(g, "Chơi ngay >>", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 5, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
      } else {
        text8.drawString3(g, "Chơi ngay >>", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 5, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
      }
      textY = textY + 20;
      g.setColor(0xC0C0C0);
      g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
      index++;
    }
  }

  private void drawPhangOnlineBrandList(Graphics g) {
    int textY = dy;
    int index = 0;
    if (tempIndex == 0) {
      dy = DrawUtils.getTopBarHeight();
    }
    text11.setBold(true);
    for (int i = 0; i < lotteryList.size(); i++) {
      g.setColor(0xA9A9A9);
      g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight());
      g.drawRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight());
      LotteryBrand brand = (LotteryBrand) lotteryList.elementAt(i);
      text11.drawString3(g, brand.name, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, textY, GameConstants.TOP_HCENTER_ANCHOR, scrollBar);
      textY = textY + 25;
      for (int j = 0; j < brand.child.size(); j++) {
        if (tempIndex == index) {
          g.setColor(0x53A4CF);
          g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight());
        }
        Lottery lottery = (Lottery) brand.child.elementAt(j);
        text8.drawString3(g, lottery.name, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        text8.drawString3(g, lottery.drawTime, GameColor.YELLOW_CODE, text8.stringWidth(lottery.name) + 10, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        if (lottery.fee > 0) {
          text8.drawString3(g, String.valueOf(lottery.fee) + "$", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - text8.stringWidth(String.valueOf(lottery.fee) + "$"), textY,
              GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
        }
        textY = textY + 20;
        g.setColor(0xC0C0C0);
        g.drawLine(0, textY - 4, GameConstants.SCREEN_WIDTH, textY - 4);
        index++;
      }
    }
    scrollBar.draw(g);

    if (percent < 100) {
      DrawUtils.getBottomBar(_manager).setCenterLabel(percent + "%");
    } else {
      DrawUtils.getBottomBar(_manager).setCenterLabel("");
    }
  }

  private void drawPhangOnlineForm(Graphics g) {
    int textY = dy;

    // Vector param = (Vector) parameters.get("Value");
    // Lottery lottery = (Lottery) param.elementAt(1);
    text11.drawString2(g, "Phang Online " + parameters.get("name"), GameColor.YELLOW_CODE, GameConstants.SCREEN_WIDTH / 2, textY, Constants.TOP_HCENTER_ANCHOR);
    textY = textY + 30;
    text8.drawString2(g, "Số phang", GameColor.WHITE_CODE, DEFAULT_TEXT_X + 10, textY, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, "Số tiền ($)", GameColor.WHITE_CODE, txtNumber.array[0].getWidth() + DEFAULT_TEXT_X + 15, textY, GameConstants.TOP_LEFT_ANCHOR);
    textY = textY + 20;
    for (int i = 0; i < 3; i++) {
      txtNumber.array[i].setPosition(DEFAULT_TEXT_X + 10, textY);
      txtBetMoney.array[i].setPosition(txtNumber.array[i].getX() + txtNumber.array[i].getWidth() + 5, textY);
      txtNumber.array[i].setVisible(true);
      txtBetMoney.array[i].setVisible(true);
      textY = textY + 20;
      if (txtNumber.array[i].isFocused() || txtBetMoney.array[i].isFocused()) {
        DrawUtils.getBottomBar(_manager).setRightLabel("Xóa");
      }
    }
    cbDe.setPosition(DEFAULT_TEXT_X + 10, textY);
    cbDe.setVisible(true);
    textY = textY + 20;
    cbLo.setPosition(DEFAULT_TEXT_X + 10, textY);
    cbLo.setVisible(true);
    textY = textY + 30;
    betButtonSprite.setPosition(DEFAULT_TEXT_X + 10, textY);
    betButtonSprite.setVisible(true);
    textY = textY + 30;
    text8.drawString2(g, "Chú ý:", GameColor.WHITE_CODE, DEFAULT_TEXT_X + 10, textY, GameConstants.TOP_LEFT_ANCHOR);
    textY = textY + 20;
    text8.drawParagraph(g, "Bạn có thể chọn cả 2 lựa chọn Lô và Đề cho những con số may mắn", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 5, DEFAULT_TEXT_X + 10, textY);
    DrawUtils.getBottomBar(_manager).setLeftLabel("Trở về");
  }

  private void drawLotteryResultOther(Graphics g) {
    int textY = dy;
    for (int i = 0; i < lotteryList.size(); i++) {
      Lottery lottery = (Lottery) lotteryList.elementAt(i);
      text8.drawString3(g, lottery.name, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR, scrollBar);
      text8.drawParagraph2(g, lottery.value.elementAt(0).toString(), GameColor.YELLOW_CODE, GameConstants.SCREEN_WIDTH, DEFAULT_TEXT_X, textY, scrollBar);
    }
  }

  private void drawLotterySchedule(Graphics g) {
    int textY = dy;
    int index = 0;
    if (tempIndex == 0) {
      dy = DrawUtils.getTopBarHeight();
    }
    text11.setBold(true);
    for (int i = 0; i < lotteryList.size(); i++) {
      g.setColor(0xA9A9A9);
      g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight());
      g.drawRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight());
      LotteryBrand brand = (LotteryBrand) lotteryList.elementAt(i);
      text11.drawString3(g, brand.name, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, textY, GameConstants.TOP_HCENTER_ANCHOR, scrollBar);
      textY = textY + 25;
      for (int j = 0; j < brand.child.size(); j++) {
        if (tempIndex == index) {
          g.setColor(0x53A4CF);
          g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight());
        }
        Lottery lottery = (Lottery) brand.child.elementAt(j);
        text8.drawString3(g, lottery.name, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        text8.drawString3(g, lottery.drawTime, GameColor.YELLOW_CODE, text8.stringWidth(lottery.name) + 10, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        if (lottery.fee > 0) {
          text8.drawString3(g, String.valueOf(lottery.fee) + "$", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - text8.stringWidth(String.valueOf(lottery.fee) + "$"), textY,
              GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
        }
        textY = textY + 20;
        g.setColor(0xC0C0C0);
        g.drawLine(0, textY - 4, GameConstants.SCREEN_WIDTH, textY - 4);
        index++;
      }
    }
    text8.drawString3(g, "", GameColor.WHITE_CODE, 0, textY, GameConstants.TOP_LEFT_ANCHOR,scrollBar);
    scrollBar.draw(g);

    if (percent < 100) {
      DrawUtils.getBottomBar(_manager).setCenterLabel(percent + "%");
    } else {
      DrawUtils.getBottomBar(_manager).setCenterLabel("");
    }
  }

  private void drawLotteryType(Graphics g) {
    int textY = dy;
    int index = 0;
    if (chooseIndex == 0) {
      dy = DrawUtils.getTopBarHeight();
    }
    for (int i = 0; i < lotteryList.size(); i++) {
      Lottery lottery = (Lottery) lotteryList.elementAt(i);
      if (chooseIndex == index) {
        g.setColor(0x53A4CF);
        g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, lottery.avatarImage.getHeight() + 2);
      }
      g.drawImage(lottery.avatarImage, 0, textY, GameConstants.TOP_LEFT_ANCHOR);
      text8.drawString3(g, lottery.name, GameColor.YELLOW_CODE, lottery.avatarImage.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      if (lottery.fee > 0) {
        text8.drawString3(g, String.valueOf(lottery.fee) + "$", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - text8.stringWidth(String.valueOf(lottery.fee) + "$"), textY,
            GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
      }
      g.setColor(0xC0C0C0);
      g.drawLine(0, textY - 4, GameConstants.SCREEN_WIDTH, textY - 4);
      textY = textY + lottery.avatarImage.getHeight() + 8;
      index++;
    }
    g.setColor(0xC0C0C0);
    g.drawLine(0, textY - 4, GameConstants.SCREEN_WIDTH, textY - 4);
    scrollBar.draw(g);
    text8.drawString3(g, "", GameColor.WHITE_CODE, 0, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);

    if (percent < 100) {
      DrawUtils.getBottomBar(_manager).setCenterLabel(percent + "%");
    } else {
      DrawUtils.getBottomBar(_manager).setCenterLabel("");
    }
  }
  
  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      DrawUtils.setBottomBar(_manager, "Menu chính", "Chọn", "Trở về");
      return;
    }
  }

  public void onRecieveData(DataPackage dataPackage) {
    Global.systemCanvas.hideAlert();
    int header = dataPackage.getHeader();
    MapDataPackage mapDataPackage = MapDataPackage.getMapPackage(dataPackage);
    type = mapDataPackage.popString("type");
    if (type.equals("-1")) {
      Global.alert.showAlert2(this, GameAlert.OK_TYPE, "Chưa có dữ liệu cho mục này", -1);
      return;
    }
    Lottery lottery = null;
    chooseIndex = 0;
    chooseParentIndex = 0;
    switch (header) {
    case LOTTERY_TYPE_LIST_RESPONSE:
      lottery = new Lottery();
      lottery.id = mapDataPackage.getString("id");
      lottery.code = mapDataPackage.getString("code");
      lottery.name = mapDataPackage.getString("name");
      ByteArray btImage = mapDataPackage.getByteArray("avatarImage");
      lottery.avatarImage = GameImage.createImage(btImage, 0, btImage.len());
      lottery.fee = NumberUtil.parseInt(mapDataPackage.getString("fee"));
      if (lottery.fee == NumberUtil.ERROR_VALUE) {
        lottery.fee = 0;
      }
      
      CATEGORY_IMAGE_HEIGHT = lottery.avatarImage.getHeight();
      packageNumber = mapDataPackage.getInt("packageNumber");
      totalPackage = mapDataPackage.getInt("totalPackage");
      percent = (packageNumber * 100) / totalPackage;
      lotteryList.addElement(lottery);
      break;
    case LOTTERY_SCHEDULE_RESPONSE:
      LotteryBrand brand = null;
      brand = getLotteryBrand(mapDataPackage.getString("parent"));
      if (brand == null) {
        brand = new LotteryBrand();
        brand.name = mapDataPackage.getString("parent");
        lotteryList.addElement(brand);
      }
      lottery = new Lottery();
      lottery.id = mapDataPackage.getString("id");
      lottery.name = mapDataPackage.getString("name");
      lottery.drawTime = mapDataPackage.getString("drawTime");
      lottery.fee = NumberUtil.parseInt(mapDataPackage.getString("fee"));
      if (lottery.fee == NumberUtil.ERROR_VALUE) {
        lottery.fee = 0;
      }
      CATEGORY_IMAGE_HEIGHT = text8.getHeight();
      packageNumber = mapDataPackage.getInt("packageNumber");
      totalPackage = mapDataPackage.getInt("totalPackage");
      percent = (packageNumber * 100) / totalPackage;
      brand.child.addElement(lottery);
      break;
    case LOTTERY_RESULT_RESPONSE:
      lottery = new Lottery();
      lottery.id = mapDataPackage.popString("id");
      lottery.code = lottery.id;
      lottery.name = mapDataPackage.popString("name");
      String result = mapDataPackage.getString("value");
      lottery.value.addElement(result);
      lotteryList.addElement(lottery);
      break;
    case LOTTERY_BET_BRAND_LIST_RESPONSE:
      brand = getLotteryBrand(mapDataPackage.getString("parent"));
      if (brand == null) {
        brand = new LotteryBrand();
        brand.name = mapDataPackage.getString("parent");
        lotteryList.addElement(brand);
      }
      lottery = new Lottery();
      lottery.id = mapDataPackage.getString("id");
      lottery.name = mapDataPackage.getString("name");
      lottery.drawTime = mapDataPackage.getString("drawTime");
      lottery.fee = NumberUtil.parseInt(mapDataPackage.getString("fee"));
      if (lottery.fee == NumberUtil.ERROR_VALUE) {
        lottery.fee = 0;
      }
      lottery.status = mapDataPackage.getString("status");
      CATEGORY_IMAGE_HEIGHT = text8.getHeight();
      packageNumber = mapDataPackage.getInt("packageNumber");
      totalPackage = mapDataPackage.getInt("totalPackage");
      percent = (packageNumber * 100) / totalPackage;
      brand.child.addElement(lottery);
      break;
    case LOTTERY_BET_BRAND_RESPONSE:
      int status = mapDataPackage.getInt("status");
      if (status == 1) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn đã phang thành công");
      } else if (status == -1) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa chọn số phang hoặc $");
      } else if (status == -2) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa chọn Lô hoặc Đề");
      } else if (status == -3) {
        Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Chương trình không tồn tại", "hoặc chưa được mở" ));
      } else {
        Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Hệ thống đang tạm thời gián đoạn", "Xin hãy thử lại sau" ));
      }
      break;
    case LOTTERY_BET_RESPONSE:
      status = mapDataPackage.getInt("status");
      if (status == 1) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn đã phang thành công");
      } else {
        Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Hệ thống đang tạm thời gian đoạn", "Xin hãy thử lại sau" ));
      }
      break;
    case LOTTERY_PHASE_RESPONSE:
      Category category = new Category();
      category.id = mapDataPackage.getString("id");
      category.code = mapDataPackage.getString("code");
      category.name = mapDataPackage.getString("name");
      category.exParam.addElement("Thời gian dự đoán:");
      category.exParam.addElement(mapDataPackage.getString("startTime") + " đến " + mapDataPackage.getString("endTime"));
      lotteryList.addElement(category);
      break;
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.DOWN:
      if (showPhangOnlineForm) {
        if (tempIndex < (txtNumber.len() * 2 + 3)) {
          tempIndex++;
          dy -= 10;
        }
        if (tempIndex < 3) {
          txtNumber.array[tempIndex].setFocused(true);
          for (int i = 0; i < tempIndex; i++) {
            txtNumber.array[i].setFocused(false);
          }
        }
        if (tempIndex > 2 && tempIndex < 6) {
          txtBetMoney.array[tempIndex - 3].setFocused(true);
          for (int i = 0; i < tempIndex - 3; i++) {
            txtBetMoney.array[i].setFocused(false);
          }
          for (int i = 0; i < 3; i++) {
            txtNumber.array[i].setFocused(false);
          }
        }
        if (tempIndex > txtNumber.len() * 2) {
          if (tempIndex == (txtNumber.len() * 2) + 1) {
            cbDe.setFocused(true);
            cbLo.setFocused(false);
          }
          if (tempIndex == (txtNumber.len() * 2) + 2) {
            cbDe.setFocused(false);
            cbLo.setFocused(true);
          }
          if (tempIndex == (txtNumber.len() * 2) + 3) {
            cbDe.setFocused(false);
            cbLo.setFocused(false);
            betButtonSprite.setDrawListener(new DrawListener() {
              public void paint(Sprite source, Graphics g) {
                text8.drawString2(g, "Phang", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
              }
            });
          }
          for (int i = 0; i < 3; i++) {
            txtNumber.array[i].setFocused(false);
            txtBetMoney.array[i].setFocused(false);
          }
        }
      }
      if (type.equals("lotteryTypeList")) {
        if (chooseIndex < lotteryList.size() - 1) {
          chooseIndex++;
        }
      }
      if (type.equals("lotteryTypeList")) {
        if (lotteryList.size() * (CATEGORY_IMAGE_HEIGHT + 7) < GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) {
          return;
        }
        if (scrollBar.canScrollUp()) {
          dy = dy - (CATEGORY_IMAGE_HEIGHT + 7);
        }
      }
      if (type.equals("XSTT_LIST") || type.equals("BT_LIST") || type.equals("CAU_LIST") || type.equals("LOTTERY_SCHEDULE_LIST") || type.equals("NGU_HANH_LIST")) {
        if (tempIndex < getAllBrandLength() - 1) {
          tempIndex++;
        }
        if (chooseParentIndex <= lotteryList.size() - 1) {
          if (chooseIndex < getBrandChildLenght(chooseParentIndex)) {
            chooseIndex++;
          }
          if (chooseIndex == getBrandChildLenght(chooseParentIndex)) {
            if (chooseParentIndex < lotteryList.size() - 1) {
              chooseParentIndex++;
              chooseIndex = 0;
            } else {
              chooseIndex = getBrandChildLenght(chooseParentIndex) - 1;
            }
          }
        }
      }
      if (type.equals("XSTT_LIST") || type.equals("BT_LIST") || type.equals("CAU_LIST") || type.equals("LOTTERY_SCHEDULE_LIST") || type.equals("NGU_HANH_LIST")) {
        if (lotteryList.size() * (CATEGORY_IMAGE_HEIGHT) > GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) {
          return;
        }
        if (scrollBar.canScrollUp()) {
          dy = dy - (CATEGORY_IMAGE_HEIGHT);
        }
      }
      if (type.equals("PHANG_ONLINE")) {
        if (chooseIndex < lotteryList.size() - 1) {
          chooseIndex++;
        }
      }
      if (type.equals("PHANG_ONLINE")) {
        if (lotteryList.size() * (CATEGORY_IMAGE_HEIGHT + 25) < GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) {
          return;
        }
        if (scrollBar.canScrollUp()) {
          dy = dy - (CATEGORY_IMAGE_HEIGHT + 25);
        }
      }
      // Lottery brand list
      if (type.equals("BET_BRAND_LIST")) {
        if (tempIndex < getAllBrandLength() - 1) {
          tempIndex++;
        }
        if (chooseParentIndex <= lotteryList.size() - 1) {
          if (chooseIndex < getBrandChildLenght(chooseParentIndex)) {
            chooseIndex++;
          }
          if (chooseIndex == getBrandChildLenght(chooseParentIndex)) {
            if (chooseParentIndex < lotteryList.size() - 1) {
              chooseParentIndex++;
              chooseIndex = 0;
            } else {
              chooseIndex = getBrandChildLenght(chooseParentIndex) - 1;
            }
          }
        }
      }
      if (type.equals("BET_BRAND_LIST")) {
        if (lotteryList.size() * (CATEGORY_IMAGE_HEIGHT) > GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) {
          return;
        }
        if (scrollBar.canScrollUp()) {
          dy = dy - (CATEGORY_IMAGE_HEIGHT);
        }
      }
      if (type.equals("XSTT") || type.equals("BT") || type.equals("CAU")) {
        if (scrollBar.canScrollUp()) {
          dy -= 5;
        } else {
          dy = textView._y;
        }
      }
      break;
    case Key.UP:
      if (showPhangOnlineForm) {
        if (tempIndex > 0) {
          tempIndex--;
          dy += 10;
        }
        if (tempIndex < 3) {
          txtNumber.array[tempIndex].setFocused(true);
          if (tempIndex == 2) {
            txtNumber.array[0].setFocused(false);
            txtNumber.array[1].setFocused(false);
          }
          if (tempIndex == 1) {
            txtNumber.array[0].setFocused(false);
            txtNumber.array[2].setFocused(false);
          }
          if (tempIndex == 0) {
            txtNumber.array[1].setFocused(false);
            txtNumber.array[2].setFocused(false);
          }
          for (int i = 0; i < txtBetMoney.len(); i++) {
            txtBetMoney.array[i].setFocused(false);
          }
        }
        if (tempIndex > 2 && tempIndex < 6) {
          if (tempIndex == 3) {
            txtBetMoney.array[0].setFocused(true);
            txtBetMoney.array[1].setFocused(false);
            txtBetMoney.array[2].setFocused(false);
          }
          if (tempIndex == 4) {
            txtBetMoney.array[1].setFocused(true);
            txtBetMoney.array[0].setFocused(false);
            txtBetMoney.array[2].setFocused(false);
          }
          if (tempIndex == 5) {
            txtBetMoney.array[2].setFocused(true);
            txtBetMoney.array[0].setFocused(false);
            txtBetMoney.array[1].setFocused(false);
          }
          for (int i = 0; i < txtNumber.len(); i++) {
            txtNumber.array[i].setFocused(false);
          }
        }
        if (tempIndex > txtNumber.len() * 2) {
          if (tempIndex == (txtNumber.len() * 2) + 1) {
            cbDe.setFocused(true);
            cbLo.setFocused(false);
          }
          if (tempIndex == (txtNumber.len() * 2) + 2) {
            cbDe.setFocused(false);
            cbLo.setFocused(true);
          }
          if (tempIndex == (txtNumber.len() * 2) + 3) {
            cbDe.setFocused(false);
            cbLo.setFocused(false);
          }
          if (tempIndex < (txtNumber.len() * 2) + 3) {
            betButtonSprite.setDrawListener(new DrawListener() {
              public void paint(Sprite source, Graphics g) {
                text8.drawString2(g, "Phang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
              }
            });
          }
          for (int i = 0; i < 3; i++) {
            txtNumber.array[i].setFocused(false);
            txtBetMoney.array[i].setFocused(false);
          }
        }
        if (tempIndex <= txtNumber.len() * 2) {
          cbLo.setFocused(false);
          cbDe.setFocused(false);
        }
      }
      if (type.equals("lotteryTypeList")) {
        if (chooseIndex > 0) {
          chooseIndex--;
        }
      }
      if (type.equals("lotteryTypeList")) {
        if (scrollBar.canScrollDown()) {
          dy = dy + (CATEGORY_IMAGE_HEIGHT + 7);
        }
      }
      if (type.equals("XSTT_LIST") || type.equals("BT_LIST") || type.equals("CAU_LIST") || type.equals("LOTTERY_SCHEDULE_LIST") || type.equals("NGU_HANH_LIST")) {
        if (tempIndex > 0) {
          tempIndex--;
        }
        if (chooseParentIndex == 0 && chooseIndex == 0) {
          break;
        }
        if (chooseIndex > 0) {
          chooseIndex--;
        } else if (chooseIndex == 0) {
          if (chooseParentIndex > 0) {
            chooseParentIndex--;
            chooseIndex = getBrandChildLenght(chooseParentIndex) - 1;
          } else if (chooseParentIndex == 0) {
            chooseIndex = getBrandChildLenght(chooseParentIndex) - 1;
          }
        }
      }
      if (type.equals("XSTT_LIST") || type.equals("BT_LIST") || type.equals("CAU_LIST") || type.equals("LOTTERY_SCHEDULE_LIST") || type.equals("NGU_HANH_LIST")) {
        if (scrollBar.canScrollDown()) {
          dy = dy + (CATEGORY_IMAGE_HEIGHT);
        }
      }
      if (type.equals("PHANG_ONLINE")) {
        if (chooseIndex > 0) {
          chooseIndex--;
        }
      }
      if (type.equals("PHANG_ONLINE")) {
        if (scrollBar.canScrollDown()) {
          dy = dy + (CATEGORY_IMAGE_HEIGHT + 25);
        }
      }

      // Lottery brand list
      if (type.equals("BET_BRAND_LIST")) {
        if (tempIndex > 0) {
          tempIndex--;
        }
        if (chooseParentIndex == 0 && chooseIndex == 0) {
          break;
        }
        if (chooseIndex > 0) {
          chooseIndex--;
        } else if (chooseIndex == 0) {
          if (chooseParentIndex > 0) {
            chooseParentIndex--;
            chooseIndex = getBrandChildLenght(chooseParentIndex) - 1;
          } else if (chooseParentIndex == 0) {
            chooseIndex = getBrandChildLenght(chooseParentIndex) - 1;
          }
        }
      }
      if (type.equals("BET_BRAND_LIST")) {
        if (scrollBar.canScrollDown()) {
          dy = dy + (CATEGORY_IMAGE_HEIGHT);
        }
      }
      if (type.equals("XSTT") || type.equals("BT") || type.equals("CAU")) {
        if (scrollBar.canScrollUp()) {
          dy += 5;
        } else {
          dy = DrawUtils.getTopBarHeight();
        }
      }
      break;
    case Key.ENTER:
      if (showPhangOnlineForm) {
        if (cbDe.isFocused()) {
          if (cbDe.isChecked()) {
            cbDe.setChecked(false);
          } else {
            cbDe.setChecked(true);
          }
        } else if (cbLo.isFocused()) {
          if (cbLo.isChecked()) {
            cbLo.setChecked(false);
          } else {
            cbLo.setChecked(true);
          }
        } else {
          // if (validateInput()) {
          currentAction++;

          Vector paramList = new Vector();
          for (int i = 0; i < txtNumber.len(); i++) {
            // Vector param = new Vector();
            if (txtNumber.array[i].isFocused()) {
              txtNumber.array[i].pointerReleased(txtNumber.array[i], txtNumber.array[i].getRealX(), txtNumber.array[i].getRealY());
            } else if (txtBetMoney.array[i].isFocused()) {
              txtBetMoney.array[i].pointerReleased(txtBetMoney.array[i], txtBetMoney.array[i].getRealX(), txtBetMoney.array[i].getRealY());
            }
            Hashtable param = new Hashtable();
            if (txtNumber.array[i].getText().length() != 0 && txtBetMoney.array[i].getText().length() != 0) {
              String number = txtNumber.array[i].getText();
              String betMoney = txtBetMoney.array[i].getText();
              param.put("number", number);
              param.put("money", betMoney);
              if (cbDe.isChecked()) {
                param.put("DE", "DE");
              }
              if (cbLo.isChecked()) {
                param.put("LO", "LO");
              }
            }
            String brandId = (String) parameters.get("id");
            param.put("id", brandId);
            String drawTime = (String) parameters.get("drawTime");
            param.put("drawTime", drawTime);
            paramList.addElement(param);
          }
          if (tempIndex == (txtNumber.len() * 2) + 3) {
            if (validateInput()) {
              int totalBetMoney = 0;
              for (int i = 0; i < paramList.size(); i++) {
                Hashtable requestParam = (Hashtable) paramList.elementAt(i);
                if (requestParam.get("number") != null) {
                  totalBetMoney = totalBetMoney + NumberUtil.parseInt(requestParam.get("number").toString());
                }
              }
              if (Global.currentUser.getMoney() < totalBetMoney) {
                Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE,StringArray.create1( "Bạn không đủ $", "Bạn có muốn nạp $ không?" ), 100);
                return;
              }
              // So phang, so $, DE, LO, TinhThanh, drawTime
              for (int i = 0; i < paramList.size(); i++) {
                Hashtable requestParam = (Hashtable) paramList.elementAt(i);
                if (requestParam.get("number") == null || requestParam.get("id") == null || requestParam.get("drawTime") == null) {
                  continue;
                }
                String strDe = (String) requestParam.get("DE");
                if (strDe == null) {
                  strDe = "";
                }
                String strLo = (String) requestParam.get("LO");
                if (strLo == null) {
                  strLo = "";
                }
                if (strDe.equals("") && strLo.equals("")) {
                  continue;
                }
                Vector submit = new Vector();
                submit.addElement(requestParam.get("number"));
                submit.addElement(requestParam.get("money"));
                submit.addElement(strDe);
                submit.addElement(strLo);
                submit.addElement(requestParam.get("id"));
                submit.addElement(requestParam.get("drawTime"));
                request(LOTTERY_BET_BRAND_REQUEST, submit);
                Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang gửi...", 99).setAlertTimeOut(10000);
                // TODO: need to sleep when send data in FrmLottery
              }
            }
          }
        }
        break;
      }
      if (type.equals("lotteryTypeList")) {
        if (!isLoadingCompleted()) {
          return;
        }
        Lottery lottery = (Lottery) lotteryList.elementAt(chooseIndex);
        if (Global.currentUser.getMoney() < lottery.fee) {
          Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create2( "Bạn không đủ $ để sử dụng", "dịch vụ này", "Bạn có muốn nạp $ không?" ), 100);
          return;
        }
        currentAction++;
        if (lottery.code.equals("PHANG_ONLINE")) {
          request(LOTTERY_BET_BRAND_LIST_REQUEST, new Vector());
          clearCategory();
        } else {
          Vector value = new Vector();
          value.addElement(lottery.id);
          value.addElement(String.valueOf(lottery.fee));
          request(LOTTERY_SCHEDULE_REQUEST, value);
          clearCategory();
        }
        break;
      } else if (type.equals("XSTT_LIST") || type.equals("BT_LIST") || type.equals("CAU_LIST") || type.equals("NGU_HANH_LIST")) {
        if (!isLoadingCompleted()) {
          return;
        }
        
        LotteryBrand brand = (LotteryBrand) lotteryList.elementAt(chooseParentIndex);
        Lottery lottery = (Lottery) brand.child.elementAt(chooseIndex);
     // Subscription
        SMS_LOTTERY_BRAND = lottery.code;
        Subscription sub = Utils.getSubcription(type, SMS_LOTTERY_BRAND);
        if (sub == null || sub.status == Subscription.EXPIRED) {
          SMSServiceCommand sms = Utils.getSMSServiceCommand(type, SMS_LOTTERY_BRAND);
          if (sms != null) {
            Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create2( "Bạn muốn đăng ký dịch vụ", "nhận tin xổ số", "hàng tuần không?" ), 10000);
            return;
          }
        }
        currentAction++;
        Vector value = new Vector();
        value.addElement(lottery.id);
        value.addElement(String.valueOf(lottery.fee));
        request(LOTTERY_RESULT_REQUEST, value);
        clearCategory();
        break;
      } else if (type.equals("BET_BRAND_LIST")) {
        Hashtable parameters1 = new Hashtable();
        // Vector param = new Vector();
        LotteryBrand brand = (LotteryBrand) lotteryList.elementAt(chooseParentIndex);
        Lottery lottery = (Lottery) brand.child.elementAt(chooseIndex);
        parameters1.put("RequestType", "PHANG_ONLINE");
        parameters1.put("formType", SHOW_FORM_BETTING_ONLINE);
        parameters1.put("id", lottery.id);
        parameters1.put("name", lottery.name);
        parameters1.put("drawTime", lottery.drawTime);
        parameters1.put("status", lottery.status);
        FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
        frmGroupBongDaXoSo.getMultiTab().setTab(0, FrmBettingMenu.instance(), parameters1);
      }
      break;
    case Key.SOFT_RIGHT:
      if (showPhangOnlineForm) {
        TextField txtTemp = null;
        for (int i = 0; i < txtNumber.len(); i++) {
          if (txtNumber.array[i].isFocused()) {
            txtTemp = txtNumber.array[i];
            break;
          }
          if (txtBetMoney.array[i].isFocused()) {
            txtTemp = txtBetMoney.array[i];
            break;
          }
        }
        if (txtTemp != null) {
          txtTemp.deleteLastChar();
          break;
        } else {
          for (int i = 0; i < txtNumber.len(); i++) {
            txtNumber.array[i].setVisible(false);
            txtBetMoney.array[i].setVisible(false);
          }
          cbDe.setVisible(false);
          cbLo.setVisible(false);
          for(int j = 0; j < txtNumber.len(); j++){
            txtNumber.array[j].setVisible(false);
          }
          chooseParentIndex = 0;
          chooseIndex = 0;
          tempIndex = 0;
          showPhangOnlineForm = false;
          currentAction = 1;
          FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
          frmGroupBongDaXoSo.getMultiTab().setTab(0, FrmBettingMenu.instance(), parameters);
          clearCategory();
          break;
        }
      }
      if (type.equals("PHANG_ONLINE")) {
        request(LOTTERY_TYPE_LIST_REQUEST, new Vector());
        clearCategory();
      }
      if (type.equals("BET_BRAND_LIST")) {
        request(LOTTERY_TYPE_LIST_REQUEST, new Vector());
        clearCategory();
      } else if (currentAction > 1) {
        if (!isLoadingCompleted()) {
          return;
        }
        clearCategory();
        back();
      } else {
        GameGlobal.nextState(FrmGroupMenu.instance(), null);
      }
      break;
    case Key.SOFT_LEFT:
      if (!isLoadingCompleted()) {
        return;
      }
      if (showPhangOnlineForm) {
        parameters.put("formType", SHOW_FORM_BETTING_BRAND_LIST);
        showPhangOnlineForm = false;
        FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
        frmGroupBongDaXoSo.getMultiTab().setTab(0, FrmBettingMenu.instance(), parameters);
      } else {
        Global.nextState(FrmGroupMenu.instance(), null);
      }
      break;
    default:
      if (showPhangOnlineForm) {
        TextField txtTemp = null;
        for (int i = 0; i < txtNumber.len(); i++) {
          if (txtNumber.array[i].isFocused()) {
            txtTemp = txtNumber.array[i];
            break;
          }
          if (txtBetMoney.array[i].isFocused()) {
            txtTemp = txtBetMoney.array[i];
            break;
          }
        }
        if (txtTemp != null) {
          txtTemp.keyReleased1(keyCode);
        }
      }
      break;
    }
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == 100 && eventType == GameAlert.YES_BUTTON) {
      FrmGroupNapXuAndExchangeGold.backForm = this;
      Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), parameters);
    }
    if (alertId == -1) {
      if (currentAction > 1) {
        clearCategory();
        back();
      }
    }
    if (alertId == 10000 && eventType == GameAlert.YES_BUTTON) {
      SMSServiceCommand sms = Utils.getSMSServiceCommand(type, SMS_LOTTERY_BRAND);
      if (sms != null) {
        SMSHandler.getInstance(this).sendMsg(sms.serviceNumber, sms.command);
      }
    }
  }

  private boolean validateInput() {
    boolean isOK = false;
    for (int i = 0; i < txtNumber.len(); i++) {
      if (txtNumber.array[i].getText().length() > 0 && txtBetMoney.array[i].getText().length() > 0) {
        isOK = true;
        break;
      }
    }
    if (!isOK) {
      Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa nhập số phang hoặc $");
      return false;
    }
    for (int i = 0; i < txtNumber.len(); i++) {
      if (txtNumber.array[i].getText().length() == 0 && txtBetMoney.array[i].getText().length() == 0) {
        continue;
      }
      
      int txtNumberInt = NumberUtil.parseInt(txtNumber.array[i].getText());
      int txtBetMoneyInt = NumberUtil.parseInt(txtBetMoney.array[i].getText());
      if (txtNumberInt == NumberUtil.ERROR_VALUE || txtBetMoneyInt == NumberUtil.ERROR_VALUE) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Số phang hoặc số $ phải là số");
        return false;
      }
    }
    if (cbDe.isChecked() || cbLo.isChecked()) {
      return true;
    } else {
      Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chọn Lô hoặc Đề");
      return false;
    }
  }

  private int getAllBrandLength() {
    int total = 0;
    if (lotteryList.size() == 0) {
      return 0;
    }
    for (int i = 0; i < lotteryList.size(); i++) {
      LotteryBrand brand = (LotteryBrand) lotteryList.elementAt(i);
      if (brand != null && brand.child.size() != 0) {
        total = total + brand.child.size();
      }
    }
    return total;
  }

  private int getBrandChildLenght(int brandIndex) {
    if (lotteryList.size() == 0) {
      return 0;
    }
    LotteryBrand brand = (LotteryBrand) lotteryList.elementAt(brandIndex);
    if (brand == null) {
      return 0;
    }
    return brand.child.size();
  }

  private LotteryBrand getLotteryBrand(String name) {
    for (int i = 0; i < lotteryList.size(); i++) {
      LotteryBrand brand = (LotteryBrand) lotteryList.elementAt(i);
      if (brand == null) {
        continue;
      }
      if (brand.name.equals(name)) {
        return brand;
      }
    }
    return null;
  }

  private void back() {
    currentAction--;
    String values = (String) logAction.get(String.valueOf(currentAction));
    StringArray requestParams = GameStringUtils.split(values, ",");
    int request = NumberUtil.parseInt(requestParams.get(0));
    Vector param = new Vector();
    for (int i = 1; i < requestParams.len(); i++)
      param.addElement(requestParams.get(i));
    request(request, param);
  }

  private void request(int header, Vector parameters1) {
    DataPackage dataPackage = DataPackage.createDataPackage1(header);
    for (int i = 0; i < parameters1.size(); i++) {
      dataPackage.putString(parameters1.elementAt(i).toString());
    }
    Global.appClient.write(dataPackage);
    logAction.put(String.valueOf(currentAction), buildLogAction(header, parameters1));
    Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(1000);
  }

  private String buildLogAction(int requestType, Vector parameters1) {
    StringBuffer sb = new StringBuffer();
    sb.append(String.valueOf(requestType));
    sb.append(",");
    for (int i = 0; i < parameters1.size(); i++) {
      sb.append(parameters1.elementAt(i).toString());
      sb.append(",");
    }
    if (sb.toString().endsWith(",")) {
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
  }

  private boolean isLoadingCompleted() {
    return packageNumber == totalPackage;
  }

  private void clearCategory() {
    lotteryList.removeAllElements();
    chooseIndex = 0;
    chooseParentIndex = 0;
    tempIndex = 0;
    dy = DrawUtils.getTopBarHeight();
  }
  
  public void onSendSMSFail() {
    Global.alert.showAlert(this, GameAlert.OK_TYPE, "Đăng ký thất bại");
  }

  public void onSendSMSSuccess() {
    Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create2( "Đăng ký thành công", "Xin mời bạn quay lại", "trong ít phút" ));
  }

  public void destroy() {
    text8 = null;
    text11 = null;
    chooseIndex = 0;
    chooseParentIndex = 0;
    tempIndex = 0;
    type = null;
    logAction = null;
    scrollBar = null;
    textView = null;
    lotteryList = null;
    currentAction = 1;
    txtNumber = null;
    txtBetMoney = null;
    cbLo = null;
    cbDe = null;
    parameters = null;
    buttonImage = null;
    betButtonSprite = null;
    SMS_LOTTERY_BRAND = null;
  }

  private class LotteryBrand {
    String name;
    Vector child = new Vector();
  }

  private class Lottery {
    String id;
    String code;
    String name;
    String drawTime;
    int fee;
    String status;
    Vector prize = new Vector();
    Vector value = new Vector();
    GameImage avatarImage;
    String parent;
  }
}
